package common.util;

import cn.hutool.http.HttpUtil;

import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 张福垒
 * @Date: 2024/10/31 10:51
 * @Description:
 * @phone: 13864677931
 */
public class PmidUtils {
    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            String phpapi = HttpUtil.get("https://pm6.yuntsg.com/editor/get-jar-vps-common?tes=1232sadasdwe3da3");
//            List<String> urls = Arrays.asList(phpapi.split("\\|"));
//
//            // 使用Collections.shuffle来随机化列表中的元素顺序
//            Collections.shuffle(urls, new Random());
//            // 取第一个元素作为URL
//            String url = urls.get(0);
//            System.out.println(url);
//        }
        String searchStr = "[ENCNTI]123";
        searchStr = searchStr.replaceAll("\\[ENCNTI\\]", "");
        System.out.println(searchStr);
    }

    public static List<Integer> searchFromPubmed(String searchStr) {
        List<Integer> list = new ArrayList<>();
        try {
            if (PublicPiYin.isContainChinese(searchStr)) {
                searchStr = YouDaoTransLate.DoCnToEnTrans(searchStr);
                searchStr = searchStr.replaceAll("\\[ENCNTI\\]", "");
            }
            String phpapi = HttpUtil.get("https://pm6.yuntsg.com/editor/get-jar-vps-common?tes=1232sadasdwe3da3");
            List<String> urls = Arrays.asList(phpapi.split("\\|"));

            // 使用Collections.shuffle来随机化列表中的元素顺序
            Collections.shuffle(urls, new Random());
            // 取第一个元素作为URL
            String url = urls.get(0);

            String fileUrl = "http://" + url + "/index/export-param-param";
            Map<String, Object> map = new HashMap<>();
            map.put("format", "pmid");
            map.put("param", URLEncoder.encode(searchStr));
            String post = HttpUtil.post(fileUrl, map);
            List<Integer> htmlPmidList = Arrays.asList(post.split("\r\n")).stream().map(o -> Integer.valueOf(o)).collect(Collectors.toList());
            List<Integer> collect = htmlPmidList.stream().limit(3000).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                list.addAll(collect);
            }
        } catch (Exception e) {
            System.out.println("获取pmid接口报错");
        }

        return list;
    }
}
